package qa.tools.controller.demo;


import lombok.extern.slf4j.Slf4j;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import qa.tools.commom.ApiResult;
import qa.tools.model.vo.AutoResultVo;
import qa.tools.utils.HuJwtUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * @ClassName DemoController
 * @Description
 * @Author Alex
 * @Date 2022/7/16 11:15
 * @Version 1.0
 */

@Slf4j
@RequestMapping("/demo")
@RestController

public class DemoController {


    @GetMapping("getInfo")
    public ApiResult getInfo(@RequestParam("runType") Integer runType) {

        if ("".equals(runType)) {
            ApiResult.failedFor(400, "runType 不能为空");
        }
        AutoResultVo autoResultVo = new AutoResultVo();
        autoResultVo.setServiceName("testService");
        autoResultVo.setBeginTime(new Date());
        autoResultVo.setEndTime(new Date());
        autoResultVo.setRunType(runType);
        autoResultVo.setTestSet(1);
        autoResultVo.setPageNum(1L);
        autoResultVo.setPageSize(50L);
        return ApiResult.succeedWith(autoResultVo, "查询成功");
    }

    @PostMapping("editInfo")
    public ApiResult editInfo(@RequestBody AutoResultVo autoResultVo) {

        if ("".equals(autoResultVo)) {
            return ApiResult.failedFor(400, "autoResultVo不能为null");
        }
        return ApiResult.succeedWith(autoResultVo, "查询成功");
    }

    @GetMapping("getName/{name}")
    public ApiResult getById(@PathVariable String name) {

        if (StringUtils.hasText(name)) {
            return ApiResult.succeedWith(200, "查询成功");
        }
        return ApiResult.failedFor(400, "name 不能为空");
    }

    @PostMapping("login")
    public ApiResult login(@RequestParam("username") String username,
                           @RequestParam("password") String password) {
        if (username.isEmpty() || password.isEmpty()) {
            return ApiResult.failedFor(400, "username 或 password不能为空");
        }

        log.info(username, password);
        Map<String, String> data = new HashMap<>(1);
        String token = HuJwtUtils.getJwtToken("1", username);
        data.put("token", token);

        return ApiResult.succeedWith(data, "登录成功");
    }

    @GetMapping("getToken")
    public ApiResult getToken(@RequestHeader("token") String token) {

        if (token.isEmpty()) {
            return ApiResult.failedFor(400, "name 不能为空");
        }

        if (!HuJwtUtils.checkToken(token)) {
            return ApiResult.failedFor(403, "无效的token");
        }
        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        return ApiResult.succeedWith(data, "查询成功");

    }

    @GetMapping("getCookie")
    public ApiResult getCookie(@CookieValue("cookie") String cookie) {
        log.info("cookie is :{}", cookie);

        if (cookie.isEmpty()) {
            return ApiResult.failedFor(400, "name 不能为空");
        }


        if (!HuJwtUtils.checkToken(cookie)) {
            return ApiResult.failedFor(401, "无效的cookie");
        }
        Map<String, Object> data = new HashMap<>();
        data.put("cookie", cookie + "_ga");
        return ApiResult.succeedWith(data, "查询成功");

    }


}
